package tk.djcrazy.instantmessage.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.domain.FriendGroup;
import tk.djcrazy.instantmessage.domain.UserEntity;
import tk.djcrazy.instantmessage.service.IFriendGroupService;
import tk.djcrazy.instantmessage.service.IFriendshipService;
import tk.djcrazy.instantmessage.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class SaveFriendInfo extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = -3648394526631100276L;
	private int viewUserId;
	private String viewNickName;
	private int viewGroupId;
	private int originalGroupId;
	private Map<String, Object> session;

	private String result = "failed";
	private String message;
	private IFriendshipService friendshipService;

	private IUserService userService;

	private IFriendGroupService friendGroupService;

	public String execute() {
		try {
			friendshipService.setFriendNickName((Integer) session.get("uid"),
					viewUserId, viewNickName);
			UserEntity user = userService
					.findById((Integer) session.get("uid"));
			if (originalGroupId != viewGroupId) {
				for (FriendGroup group : user.getGroups()) {
					boolean jump = false;
					if (group.getId() == originalGroupId) {
						for (UserEntity entity : group.getContentUsers()) {
							if (entity.getId() == viewUserId) {
								group.getContentUsers().remove(entity);
								friendGroupService.save(group);
								jump = true;
								break;
							}
						}
					}
					if (jump) {
						break;
					}
				}
				for (FriendGroup group : user.getGroups()) {
					if (group.getId() == viewGroupId) {
						System.out
								.println("找到新的位置，添加之:" + group.getGroupName());
						System.out.println("原个数："
								+ group.getContentUsers().size());
						group.getContentUsers().add(
								userService.findById(viewUserId));
						friendGroupService.save(group);
						System.out.println("新个数："
								+ group.getContentUsers().size());
						break;
					}
				}
			}
			userService.save(user);
			result = "success";
		} catch (Exception e) {
			e.printStackTrace();
			result = "failed";
		}
		return SUCCESS;
	}

	public int getViewUserId() {
		return viewUserId;
	}

	public void setViewUserId(int viewUserId) {
		this.viewUserId = viewUserId;
	}

	public String getViewNickName() {
		return viewNickName;
	}

	public void setViewNickName(String viewNickName) {
		this.viewNickName = viewNickName;
	}

	public int getViewGroupId() {
		return viewGroupId;
	}

	public void setViewGroupId(int viewGroupId) {
		this.viewGroupId = viewGroupId;
	}

	public IFriendshipService getFriendshipService() {
		return friendshipService;
	}

	public void setFriendshipService(IFriendshipService friendshipService) {
		this.friendshipService = friendshipService;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public int getOriginalGroupId() {
		return originalGroupId;
	}

	public void setOriginalGroupId(int originalGroupId) {
		this.originalGroupId = originalGroupId;
	}

	public IFriendGroupService getFriendGroupService() {
		return friendGroupService;
	}

	public void setFriendGroupService(IFriendGroupService friendGroupService) {
		this.friendGroupService = friendGroupService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
